﻿import QtQuick 2.0
import CCustomClass 1.0
import "../chromatogram"

// 图表的标题
// CustomChart的子项，请不要单独食用
Item {

    id: root

    height: 30*dp
    width: parent.width

    // 标题的文字
    CustomText{
        visible: showChartTitle
        font.pixelSize: title_fontsize
        font.bold: true
        text: title
        anchors.verticalCenter: parent.verticalCenter
    }

    Component.onCompleted: {
        for(let i = 0; i < row_btn.children.length; i ++){
            row_btn.children[i].button_border = "#AAA"
            row_btn.children[i].button_color  = "transparent"
            row_btn.children[i].button_color_select = "#AAA"
            row_btn.children[i].text_color_select = "white"
            row_btn.children[i].button_color_hover = "#CCC"
            row_btn.children[i].text_color_hover = "white"
            row_btn.children[i].radius = 5*dp
            row_btn.children[i].text_color = "#888"
        }
    }

    Row{

        id: row_btn

        visible: showTitleBtn

        height: 30*dp
        anchors.right: parent.right
        anchors.rightMargin: 10*dp
        anchors.verticalCenter: parent.verticalCenter
        spacing: 5*dp

        CustomButton{
            id: btn_scale
            title: "放大模式"
            autoWidth: true; height: 30*dp
            onClicked: {
                isSelected = !isSelected
                if(isSelected){
                    lastXRange = cChartInterface.getShowRange()[1]-cChartInterface.getShowRange()[0]

                    mouseWheelXControl = 0
                    mouseWheelYControl = 0
//                    btn_xScale.isSelected = false
                }else{                          // 退出恢复正常模式
                    mouseWheelXControl = 1
                    mouseWheelYControl = -1
                    resetRange()
                }
            }
            CustomTooltip{
                visible: parent.isHovered
                text: "使用滚轮进行缩放"
            }
        }
//        CustomButton{
//            id: btn_xScale
//            title: "水平缩放"
//            autoWidth: true; height: 30*dp
//            onClicked: {
//                isSelected = !isSelected
//                if(btn_scale.isSelected){
//                    resetRange()
//                }
//                if(isSelected){
//                    mouseWheelXControl = 0
//                    mouseWheelYControl = -1
//                    btn_scale.isSelected = false
//                }else{                          // 退出恢复正常模式
//                    mouseWheelXControl = 1
//                    mouseWheelYControl = -1
//                }
//            }
//            CustomTooltip{
//                visible: parent.isHovered
//                text: "使用滚轮调节网格密度"
//            }
//        }
        CustomButton{
            id: button_reset
            autoWidth: true; height: 30*dp
            title: "复位"
            onClicked: {
                btn_scale.isSelected = false
//                btn_xScale.isSelected = false
                mouseWheelXControl = 1
                mouseWheelYControl = -1
                resetRange()
            }
        }

        // 操作项
        CustomButton{
            id: button_menu
            width: 30*dp; height: 30*dp
            radius: 5*dp
            button_icon: "qrc:/picture/menu_0.png"
            onClicked: {
                layout_menu.visible = !layout_menu.visible
            }
        }
    }


    Rectangle{
        id: layout_menu
        visible: false
        height: column_list.implicitHeight + 40*dp
        width: column_list.implicitWidth + 40*dp
        x: parent.width - width
        y: parent.height + 10*dp
        border.color: "#AAA"
        Column{
            id: column_list
            anchors.fill: parent
            anchors.margins: 20*dp
            spacing: 10*dp
            CustomComboBox{
                title: "时间单位"
                model: ["分钟(min)", "秒(s)"]
                onClickIndex: {
                    xTitleMode = index
                }
            }
            CustomComboBox{
                title: "x轴间隔"
                model: ["10s", "20s", "30s", "1min", "3min", "5min"]
                index: 3
                onClickIndex: {
                    let tickSpace = 60
                    if(getText() === "10s")          tickSpace = 10
                    else if(getText() === "20s")     tickSpace = 20
                    else if(getText() === "30s")     tickSpace = 30
                    else if(getText() === "1min")    tickSpace = 60
                    else if(getText() === "3min")    tickSpace = 180
                    else if(getText() === "5min")    tickSpace = 300
                    cChartInterface.setMeshTick(CCustomChart.AXIS_X, CCustomChart.FIXED_SPACE, tickSpace)     // 感觉还是从C++注册过来的枚举好用呢…为啥QML的枚举都没有自动不全……
                    cChartInterface.refresh()
                }
            }
            CustomComboBox{
                title: "y轴网格数"
                index: 3
                model: [5, 6, 8, 10, 12, 16, 20]
                onClickIndex: {
                    cChartInterface.setMeshTick(CCustomChart.AXIS_Y, CCustomChart.FIXED_NUM, Number(getText()))     // 感觉还是从C++注册过来的枚举好用呢…为啥QML的枚举都没有自动不全……
                    cChartInterface.refresh()
                }
            }
            Row{
                height: 40*dp
                Text{
                    text: "水平缩放"
                    width: 80*dp
                    font.pixelSize: config.fontsize
                    anchors.verticalCenter: parent.verticalCenter
                }
                CustomSlideBar{
                    width: 160*dp
                    percentage: 1
                    anchors.verticalCenter: parent.verticalCenter
                    onPercentageChanged: {
                        var showRange = cChartInterface.getShowRange()
                        var x1 = showRange[0]
                        var x2 = showRange[1]
                        var y1 = showRange[2]
                        var y2 = showRange[3]
                        let rangeSize = (1-minXRangeSize) * percentage + minXRangeSize
                        var xCenter = (x1+x2) / 2
                        x1 = xCenter - rangeSize / 2
                        x2 = xCenter + rangeSize / 2
                        cChartInterface.setShowRange(x1, x2, y1, y2)
                        cChartInterface.refresh()

                        scrollBar_H.r1 = 1-y2
                        scrollBar_H.r2 = 1-y1
                        scrollBar_V.r1 = x1
                        scrollBar_V.r2 = x2

                        lastXRange = x2-x1

                    }
                }
            }
//            CustomButton{
//                title: "导出图片"
//                defaultMode2: true
//                onClicked: {
//                    box_exportImage.show()
//                }
//            }
        }
    }


}
